<?php 

class CW_Upload{
     
	private $fileType=array();
	private $fileMaxSize = 2048;
	private $saveFilePath = "";
	
	function CW_Upload(){
		$param = get_app_config('upload');
		$this->setParam($param);
		//var_dump($param);
	}
	

	/**
	 * 
	 * @param array $param
	 */
	function setParam($param){
		 $this->fileType = $param["fileType"];
		 $this->fileMaxSize = $param["fileMaxSize"];
		 $this->saveFilePath = $param["saveFilePath"];
	}
	
	
	/**
	 * 上传函数
	 * @param Array $file
	 * @return  array 
	 * (
	 *  1. 文件大小超出了服务器的空间大小
	 *  2. 要上传的文件大小超出浏览器限制
	 *  3.文件仅部分被上传  
	 *  4.没有找到要上传的文件
	 *  5.文件格试不对、文件太大
	 * )
	 */
	function upload($files){
		 $dateDir = date("ymd")."/";
		 $return  = NUll;
		 if(!empty($files)){
		 	  foreach($files as $key=>$file){
		 	  	    $fileName = $file["name"];
		 	  	    $return["$fileName"]["num"] = 0 ;
		 	  	    $fileTempName = $file["tmp_name"];
		 	  	    $path = $this->saveFilePath.$dateDir.time().$key.".".$this->getExtension($fileName);
		 	  	    if($file["error"]==0 && $this->valFileExt($file) && $this->valFileSize($file)){
                     	if(!is_dir(ROOT_PATH.$this->saveFilePath.$dateDir)){
                     		$mkdir_file_dir = mkdir(ROOT_PATH.$this->saveFilePath.$dateDir,0777);
                     	}
                     	move_uploaded_file($fileTempName,ROOT_PATH.$path);
		 	  	    }else{
		 	  	    	switch ($file["error"]){
		 	  	    		case 1: $return["$fileName"]["num"] = 1;
		 	  	    		break;
		 	  	    		case 2 : $return["$fileName"]["num"]= 2;
		 	  	    		break ;
		 	  	    		case 3: $return["$fileName"]["num"] = 3;
		 	  	    		break;
		 	  	    		case 4: $return["$fileName"]["num"] = 4;
		 	  	    		default: $return["$fileName"]["num"] = 5;
		 	  	    	}
		 	  	    }
		 	  	    
		 	  	    $return["$fileName"]["url"] = $path;
		 	  }
		 }
		 return $return ;
	}
	
	/**
	 * 
	 * @param array $file
	 * @return boolean
	 */
	function valFileSize($file){
		$val = false;
		$size = $this->fileMaxSize * 1024;
		
		if($size >= $file["size"]){
			$val = true;
		}
		
		return $val;
	}
	
	
	/**
	 * 
	 * @param unknown $file
	 * @return boolean
	 */
	function valFileExt($file){
		 $val = false;
		 $fType = $this->getExtension($file["name"]);
		 if(in_array($fType,$this->fileType)){
		 	 $val = true;
		 }
		 
		 return $val;
	}
	
	
    /**
     * 获取文件的扩展名
     * @param String $file
     * @return mixed
     */
	function getExtension($fileUrl){
		return pathinfo($fileUrl, 4);
	}
	
	
	function download($file) {
			//这是下载文件的函数,$file是文件路径.
			//First, see if the file exists
			if (!is_file($file)) { die("<b>404 File not found!</b>"); }
		
			//Gather relevent info about file
			$len = filesize($file);
			$filename = basename($file);
			$file_extension = strtolower(substr(strrchr($filename,"."),1));
		
			//This will set the Content-Type to the appropriate setting for the file
			switch( $file_extension ) {
				case "pdf": $ctype="application/pdf"; break;
				case "exe": $ctype="application/octet-stream"; break;
				case "zip": $ctype="application/zip"; break;
				case "doc": $ctype="application/msword"; break;
				case "xls": $ctype="application/vnd.ms-excel"; break;
				case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
				case "gif": $ctype="image/gif"; break;
				case "png": $ctype="image/png"; break;
				case "jpeg":
				case "jpg": $ctype="image/jpg"; break;
				case "mp3": $ctype="audio/mpeg"; break;
				case "wav": $ctype="audio/x-wav"; break;
				case "mpeg":
				case "mpg":
				case "mpe": $ctype="video/mpeg"; break;
				case "mov": $ctype="video/quicktime"; break;
				case "avi": $ctype="video/x-msvideo"; break;
		
				//The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files)
				case "php":
				case "htm":
				case "html":
				case "txt": die("<b>Cannot be used for ". $file_extension ." files!</b>"); break;
		
				default: $ctype="application/force-download";
			}
		
			//Begin writing headers
			header("Pragma: public");
			header("Expires: 0");
			header("Cache-Control: must-rev alidate, post-check=0, pre-check=0");
			header("Cache-Control: public");
			header("Content-Description: File Transfer");
		
			//Use the switch-generated Content-Type
			header("Content-Type: $ctype");
		
			//Force the download
			$header="Content-Disposition: attachment; filename=".$filename.";";
			header($header);
			header("Content-Transfer-Encoding: binary");
			header("Content-Length: ".$len);
			@readfile($file);
			exit;
	}
}

?>